package com.example.liverock.androidalltest;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;

import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;

public class ProgressBarActivity extends Activity {


    /**
     * 功能：实现页面完全载入前，显示一个圆形进度条，当页面载入后，隐藏该进度条
     */

    // 全局变量；
    int mProgressStatus = 0;        // 进度条的进度值；
    Handler mHandler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_progress_bar);


        final ProgressBar horizonProgress = (ProgressBar)findViewById(R.id.horizonProgress);
        final ProgressBar circleProgress = (ProgressBar)findViewById(R.id.circleProgress);
        //进度条的进度减少，当参数值为正数时，进度增加，为负数时，进度减少
        circleProgress.incrementProgressBy(-1);
        circleProgress.setVisibility(View.VISIBLE);     // 设置圆形进度条可见

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true)
                {
                    mProgressStatus = doWork();
                    Message message = new Message();
                    if (mProgressStatus < 100)
                    {
                        message.what = 0x111;
                        mHandler.sendMessage(message);
                    }
                    else
                    {
                        message.what = 0x110;
                        mHandler.sendMessage(message);
                    }
                }
            }
            private int doWork()
            {
                mProgressStatus += Math.random() * 10;
                try{
                    Thread.sleep(200);
                }catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
                return mProgressStatus;
            }
        }).start();
        mHandler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                if(msg.what==0x111){
                    horizonProgress.setProgress(mProgressStatus);	//更新进度
                    circleProgress.setProgress(100-mProgressStatus);
                }else{
                    horizonProgress.setVisibility(View.GONE);	//设置进度条不显示，并且不占用空间
                    circleProgress.setVisibility(View.GONE);//设置进度条不显示，并且不占用空间
                }
            }
        };
    }

}
